﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Core;
using Domain.Model.Enums;
using Domain.Repository.Sugar;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using SqlSugar.IOC;
namespace Domain.Infrastructure.ServiceExtensions
{
    public static class SqlsugarSetup
    {
        public static void AddSqlsugarSetup(this IServiceCollection services)
        {
            SugarIOCConfig.GetIocConfigs()?.ForEach(ioc => { services.AddSqlSugar(ioc); });
            //AOP 统一配置  禁止循环，只能声名一次
            services.ConfigurationSugar(db =>
            {
                SugarIOCConfig.GetIocConfigs()?.ForEach(ioc => {
                    SqlSugarProvider curConnection = db.GetConnection(ioc.ConfigId);
                    if (curConnection != null)
                    {
                        curConnection.Aop.OnLogExecuting = (sql, p) =>
                        {
                            Console.WriteLine(sql);
                        };
                    }
                });
            });
        }
    }
}
